package features.advance.jdk.io.blocking;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 *
 *  UDP/IP + BIO
 *
 * @author LIN
 * @date 2021-07-09
 */
public class UDPServer {

    public static void main(String[] args) throws Exception{
        DatagramSocket serverSocket = new DatagramSocket(8082);
        byte[] buffer = new byte[65507];
        DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
        DatagramSocket clientSocket = new DatagramSocket();
        String data = "UDP hello world!!!";
        byte[] datas = data.getBytes();
        DatagramPacket packet = new DatagramPacket(datas, datas.length, InetAddress.getByName("localhost"), 8082);
        clientSocket.send(packet);
        serverSocket.setSoTimeout(3000);
        serverSocket.receive(receivePacket);

        System.out.println(new String(buffer).subSequence(0,new String(buffer).indexOf("\u0000")));

    }


}
